package com.xcm.sso;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xcm.cache.RedisCacheDao;
import com.xcm.constant.CacheConstant;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 单点帮助类
 *
 * @author 林强
 * @date 2018-04-12 11:15
 */
public class SSOUtils {

    /**
     * 根据系统标志获取系统登录页面名称
     *
     * @param system
     * @return
     */
    public static String getSystemLoginPageBySystem(SSOProperties ssoProperties, String system) {
        if (StringUtils.isBlank(system)) {
            return SSOContants.PATH.LOGIN_PAGE_PORTAL.getValue();
        }
        if (system.equals(ssoProperties.getSystemSignXcips())) {
            return SSOContants.PATH.LOGIN_PAGE_XCIPS.getValue();
        }
        if (system.equals(ssoProperties.getSystemSignDmp())) {
            return SSOContants.PATH.LOGIN_PAGE_DMP.getValue();
        }
        return SSOContants.PATH.LOGIN_PAGE_PORTAL.getValue();
    }

    /**
     * 创建ticket过期时间
     *
     * @param start   开始时间
     * @param timeout 超时分钟数
     * @return
     */
    public static Timestamp createRecoverTime(Timestamp start, int timeout) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        cal.add(Calendar.SECOND, timeout);
        return new Timestamp(cal.getTimeInMillis());
    }

    /**
     * 创建统一账户管理系统登录地址
     *
     * @param gotoURL 回到地址
     * @return 登录地址
     */
    public static String createPortalLoginUrl(SSOProperties ssoProperties, String gotoURL) {
        String ssoLoginUrl = null;
        try {
            ssoLoginUrl = SSOContants.PATH.SSO_METHOD_PRE_LOGIN.getValue()
                    + "?" + SSOContants.PLATFORM_TAG + "=" + ssoProperties.getSystemSignPortal()
                    + "&" + SSOContants.SET_COOKIE_URL + "=" + DESUtils.encrypt(SSOContants.PATH.PORTAL_METHOD_SETCOOKIE.getValue(), ssoProperties.getSecretKey())
                    + "&" + SSOContants.REDIRECT_URL + "=" + DESUtils.encrypt(gotoURL, ssoProperties.getSecretKey());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ssoLoginUrl;
    }

    /**
     * 验证ticket是否过期
     *
     * @param redisCacheDao redis
     * @param decodedTicket ticket(解密后)
     * @return 过期返回true, 反之false
     */
    public static boolean isTicketExpire(RedisCacheDao redisCacheDao, String decodedTicket, SSOProperties ssoProperties) {
        Object ticketObj = redisCacheDao.getCache(ssoProperties.getSsoTicketsCacheName(), decodedTicket);
        if (null == ticketObj) {
            return true;
        }
        JSONObject ticketJson = JSONObject.parseObject(ticketObj.toString());
        Ticket ticket = JSON.toJavaObject(ticketJson, Ticket.class);
        Timestamp recoverTime = ticket.getRecoverTime();
        long expire = recoverTime.getTime();
        long now = System.currentTimeMillis();
        return now > expire;
    }
}
